Creating Cams Programmatically
The BasicMotion_CreateCamTableOnline.project
sample project is located in the installation directory of CODESYS under ..\CODESYS SoftMotion\Examples
.
The example shows how you can use IEC code to create and use a cam. The following points are covered here:
A cam created in the cam editor is created again using IEC code.
In one case, the cam disk is generated in the bus task.
In the other case, the cam disk is generated in a separate task.
Then, all three cams are run with the same master axis and different slave axes. The movement of the three slaves is identical.
The cam generated in the IEC code is displayed in the visualization.
For more information about the programmatic creation of cams, see: Creating Cams Programmatically
Structure of the application
The application consists of the following components:
CamOffline
The cam created in the cam editor which should be created again using IEC code.
PLC_PRG
The program consists of a simple state machine and, logically, three parts. In the first and second parts, a cam is created in IEC code. In the third part, the previously created cams and an identical cam created offline are run.
Part 1:
In the first state
STATE_CREATE_ONLINE_TABLE
, a cam is created in IEC code. The SMCB.CamBuilder function block from theSM3_CamBuilder
library is used for this purpose. Three segments are added: a Poly5 element followed by a Line element and another Poly5 element. The Builder generates a CamOnline cam from this. From this point on, the cam is created in IEC code and can be used in the same way as the CamOffline cam created offline in the cam editor.Part 2:
In the
STATE_INIT_ONLINE_TABLE_MULTITASK
,STATE_START_CREATE_ONLINE_TABLE_MULTITASK
, andSTATE_READ_ONLINE_TABLE_MULTITASK
states, the creation of the cam is started in another task and then it is read.Part 3:
In the
STATE_POWER_DRIVES
state, the virtual axes for the master and the three slave axes are initialized. The cams are then selected usingMC_CamTableSelect
in theSTATE_SELECT_TABLE
state and executed in the finalSTATE_MOVEMENT
state. The master axis is moved usingMC_MoveVelocity
and is used as the master for the three cams.
MultitaskCamBuilder
A program which generates a cam as described in Part 1 of
PLC_PRG
. The only difference here is that a multitask/multicore-safe variant of the cam is used.GVL
The multitask/multicore-safe variant of the cam is instantiated here.
Visualization
The visualization contains the
SMC_Visu_CamDisplayer
element for displaying cams. The cam generated in Part 1 ofPLC_PRG
is displayed.CamDisplayerSettings
Additional elements for displaying the cam in
Visualization
Trace
You can use the trace to track the movement of the virtual axes. The current position of the master is recorded, as well as the position, velocity, and acceleration of the three slave axes.
Commissioning
Build the application and download it to a controller.
Start the trace and the application.
Observe the sampling trace.
Display of the generated cam in the visualization